package in.muton.edushare.panels.folder;

import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.Users;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.folder.FolderHelper;

public class FolderListPanel extends EdusharePanel {

	private static final long serialVersionUID = -8214925106843679129L;
	private IModel<FileDirectories> dirModel;

	public FolderListPanel(String id, IModel<FileDirectories> fileDirectoryModel){
		super(id, fileDirectoryModel);
		this.dirModel = fileDirectoryModel;
		add(CSSPackageResource.getHeaderContribution(FolderListPanel.class, "folder.css"));
		boolean isValidUser = isValidUser(dirModel.getObject(), getLoggedInUser());
		if(isValidUser){
			loadList();
		}else{
			//show warning
		}
	}

	private boolean isValidUser(FileDirectories object, Users loggedInUser) {
		// TODO Auto-generated method stub
		return true;
	}

	private void loadList() {
		
		PageableListView<FileDirectories> plv = 
			new PageableListView<FileDirectories>
				(		"dirItem", 
						new PropertyModel<List<FileDirectories>>(FolderListPanel.this, "dirChildren"),
						ApplicationConstants.PAGE_SIZE)
			{

				private static final long serialVersionUID = -2897179508216652525L;

				@Override
				protected void populateItem(ListItem<FileDirectories> item) {
					item.add(new Label("dirLink", item.getModelObject().getDirectoryName()));
					item.add(new Label("dirLastUpdated", item.getModelObject().getUpdateTimestamp()+""));
				}
					
			};
			
		add(plv);

	}

	public List<FileDirectories> getDirChildren() {
		if(this.dirModel == null || this.dirModel.getObject()==null)
			return null;
		
		List<String> userVisibility = FolderHelper.getUserVisibilityUnderDir(getLoggedInUser(), this.dirModel.getObject());
		List<FileDirectories> fdl = new FileDirectoriesDAO(getORMInterface()).getChildren(this.dirModel.getObject(), userVisibility);
		
		return fdl;
	}
}
